package vn.co.thn.web.demo_app_engine.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import vn.co.thn.web.demo_app_engine.bean.BeanBase;
import vn.co.thn.web.demo_app_engine.jpa.Category;
import vn.co.thn.web.demo_app_engine.model.CategoryModel;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

@SuppressWarnings("serial")
public class CategoryAdminController extends ControllerBase {
	private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(request, response);
		String query = request.getQueryString();
		if (query != null && query.trim().length() > 0) {
			if (request.getParameter("action") != null && request.getParameter("action").equals("delete")) {
				CategoryModel model = new CategoryModel();
				model.setBlobstoreService(blobstoreService);
				model.delete(request.getParameter("key"));
				
				List<Category> lst = new CategoryModel().getAll();
				response.setContentType("text/html;charset=UTF-8");
				PrintWriter out = response.getWriter();
				if (lst.size() > 0) {
					out.println("<table border=\"1\">");
					for (Category obj : lst) {
						out.println("<tr>");
						out.println("<td>" + obj.getKey() + "</td>");
						out.println("<td>" + obj.getId() + "</td>");
						out.println("<td>" + obj.getName() + "</td>");
						out.println("<td><img src=\" " + obj.getImage_Url() + " \"</td>");
						out.println("<td>" + obj.getDelete() + "</td>");
						out.println("<td><a href=\"JavaScript:ajax('?action=delete&key=" + obj.getKey() + "','htmldata')\" >Delete</a></td>");
						out.println("<td><a href=\"?action=edit&key=" + obj.getKey() + "\">Edit</a></td>");
						out.println("</tr>");
					}

					out.println("</table>");
				} else {
					out.println("");
				}
				return;
			}
			Forward("admin_category_update");
		} else {
			CategoryModel categoryModel = new CategoryModel();
			request.setAttribute("datalist", categoryModel.getAll());
			Forward("admin_category");
		}
	}
	

	@SuppressWarnings("deprecation")
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);
		 Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
		 BlobKey blobKey = blobs.get("Image_Url");
		 String key_image=blobKey.getKeyString();
		CategoryModel model=new CategoryModel();
		int port =request.getServerPort();
		String host;
		if(port==80){
			host=	request.getServerName();
		}else{
			host=	request.getServerName()+":"+port;
		}
		try {
			Category category=BeanBase.RequestToObject(request, Category.class);
			category.setId(3);
			category.setKey_image(key_image);
			category.setImage_Url(request.getScheme()+"://"+host+"/imageview?image_url="+key_image);
			category.setDelete(1);
			model.add(category);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Redirect("/admin/category");
	}
}
